{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "b3844753-aeb1-457f-b2dc-6f6f1bf64891",
   "metadata": {},
   "outputs": [],
   "source": [
    "from huggingface_hub import HfApi, ModelFilter\n",
    "hf_api = HfApi()\n",
    "# models = api.list_models()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "ecf31380-527b-433e-b0cb-9aa32040d6f7",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/ben/anaconda3/envs/newgen/lib/python3.11/site-packages/huggingface_hub/utils/endpoint_helpers.py:247: FutureWarning: 'ModelFilter' is deprecated and will be removed in huggingface_hub>=0.24. Please pass the filter parameters as keyword arguments directly to the `list_models` method.\n",
      "  warnings.warn(\n"
     ]
    }
   ],
   "source": [
    "models = hf_api.list_models(\n",
    "    filter=ModelFilter(\n",
    "\t\ttask=\"text-generation\",\n",
    "\t\t# library=\"pytorch\",\n",
    "\t\t# trained_dataset=\"imagenet\"\n",
    "\t)\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "e0dd16c5-cdcf-4a8b-9908-9fa8acc036d6",
   "metadata": {},
   "outputs": [],
   "source": [
    "model_list = []\n",
    "for model in models:\n",
    "    model_list.append(model)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "88d90424-18fb-4e36-8a7e-faea222a9843",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "73813"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(model_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "ba50e0b2-4a71-4e78-81b5-19bf3d5b6de5",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/ben/anaconda3/envs/newgen/lib/python3.11/site-packages/huggingface_hub/utils/endpoint_helpers.py:247: FutureWarning: 'ModelFilter' is deprecated and will be removed in huggingface_hub>=0.24. Please pass the filter parameters as keyword arguments directly to the `list_models` method.\n",
      "  warnings.warn(\n"
     ]
    }
   ],
   "source": [
    "models = hf_api.list_models(\n",
    "    filter=ModelFilter(\n",
    "\t\ttask=\"summarization\",\n",
    "\t\t# library=\"pytorch\",\n",
    "\t\t# trained_dataset=\"imagenet\"\n",
    "\t)\n",
    ")\n",
    "summarization_list = []\n",
    "for model in models:\n",
    "    summarization_list.append(model)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "209813cd-8077-41ae-9e2c-ebdaec4f726f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1695"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(summarization_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "d0f98fdd-a956-41cc-874b-a86eb0464046",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "ModelInfo(id='google-t5/t5-11b', author='google-t5', sha='90f37703b3334dfe9d2b009bfcbfbf1ac9d28ea3', created_at=datetime.datetime(2022, 3, 2, 23, 29, 4, tzinfo=datetime.timezone.utc), last_modified=datetime.datetime(2023, 1, 2, 16, 15, 50, tzinfo=datetime.timezone.utc), private=False, gated=False, disabled=None, downloads=18937, likes=49, library_name='transformers', tags=['transformers', 'pytorch', 'tf', 't5', 'text2text-generation', 'summarization', 'translation', 'en', 'fr', 'ro', 'de', 'multilingual', 'dataset:c4', 'arxiv:1805.12471', 'arxiv:1708.00055', 'arxiv:1704.05426', 'arxiv:1606.05250', 'arxiv:1808.09121', 'arxiv:1810.12885', 'arxiv:1905.10044', 'arxiv:1910.09700', 'license:apache-2.0', 'autotrain_compatible', 'has_space', 'text-generation-inference', 'region:us'], pipeline_tag='translation', mask_token=None, card_data=None, widget_data=None, model_index=None, config=None, transformers_info=None, siblings=[RepoSibling(rfilename='.gitattributes', size=None, blob_id=None, lfs=None), RepoSibling(rfilename='README.md', size=None, blob_id=None, lfs=None), RepoSibling(rfilename='config.json', size=None, blob_id=None, lfs=None), RepoSibling(rfilename='pytorch_model.bin', size=None, blob_id=None, lfs=None), RepoSibling(rfilename='spiece.model', size=None, blob_id=None, lfs=None), RepoSibling(rfilename='tf_model.h5', size=None, blob_id=None, lfs=None), RepoSibling(rfilename='tokenizer.json', size=None, blob_id=None, lfs=None)], spaces=None, safetensors=None)"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "summarization_list[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "d112bec2-cf63-4b13-894f-1836741cf4c1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'author': 'google-t5',\n",
       " 'last_modified': datetime.datetime(2023, 1, 2, 16, 15, 50, tzinfo=datetime.timezone.utc),\n",
       " 'downloads': 18937,\n",
       " 'pipeline_tag': 'translation',\n",
       " 'id': 'google-t5/t5-11b'}"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def extract_json(model_info): \n",
    "    return dict(\n",
    "        author=model_info.author,\n",
    "        last_modified=model_info.last_modified,\n",
    "        downloads=model_info.downloads,\n",
    "        pipeline_tag=model_info.pipeline_tag,\n",
    "        id=model_info.id\n",
    "    )\n",
    "    \n",
    "extract_json(summarization_list[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "6e10b183-31c0-46fd-9527-3421c36e7f5f",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "\n",
    "df = pd.DataFrame([extract_json(model) for model in summarization_list])\n",
    "df.last_modified = df.last_modified.dt.date"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "e65bde9a-1a70-4cd8-a1e5-65c4f61983b0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>downloads</th>\n",
       "      <th>id</th>\n",
       "      <th>last_modified</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>224</th>\n",
       "      <td>3513936</td>\n",
       "      <td>facebook/bart-large-cnn</td>\n",
       "      <td>2024-02-13</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>305</th>\n",
       "      <td>840765</td>\n",
       "      <td>mrm8488/bert2bert_shared-spanish-finetuned-sum...</td>\n",
       "      <td>2023-05-02</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>325</th>\n",
       "      <td>550781</td>\n",
       "      <td>philschmid/bart-large-cnn-samsum</td>\n",
       "      <td>2022-12-23</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>342</th>\n",
       "      <td>433806</td>\n",
       "      <td>sshleifer/distilbart-cnn-12-6</td>\n",
       "      <td>2021-06-14</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>242</th>\n",
       "      <td>152363</td>\n",
       "      <td>google/pegasus-xsum</td>\n",
       "      <td>2023-01-24</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>234</th>\n",
       "      <td>74174</td>\n",
       "      <td>google/pegasus-cnn_dailymail</td>\n",
       "      <td>2023-01-24</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>273</th>\n",
       "      <td>58214</td>\n",
       "      <td>knkarthick/MEETING_SUMMARY</td>\n",
       "      <td>2023-03-27</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1415</th>\n",
       "      <td>46905</td>\n",
       "      <td>Falconsai/text_summarization</td>\n",
       "      <td>2024-02-17</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>313</th>\n",
       "      <td>46627</td>\n",
       "      <td>nsi319/legal-pegasus</td>\n",
       "      <td>2021-03-11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>236</th>\n",
       "      <td>36666</td>\n",
       "      <td>google/pegasus-large</td>\n",
       "      <td>2023-01-24</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      downloads                                                 id  \\\n",
       "224     3513936                            facebook/bart-large-cnn   \n",
       "305      840765  mrm8488/bert2bert_shared-spanish-finetuned-sum...   \n",
       "325      550781                   philschmid/bart-large-cnn-samsum   \n",
       "342      433806                      sshleifer/distilbart-cnn-12-6   \n",
       "242      152363                                google/pegasus-xsum   \n",
       "234       74174                       google/pegasus-cnn_dailymail   \n",
       "273       58214                         knkarthick/MEETING_SUMMARY   \n",
       "1415      46905                       Falconsai/text_summarization   \n",
       "313       46627                               nsi319/legal-pegasus   \n",
       "236       36666                               google/pegasus-large   \n",
       "\n",
       "     last_modified  \n",
       "224     2024-02-13  \n",
       "305     2023-05-02  \n",
       "325     2022-12-23  \n",
       "342     2021-06-14  \n",
       "242     2023-01-24  \n",
       "234     2023-01-24  \n",
       "273     2023-03-27  \n",
       "1415    2024-02-17  \n",
       "313     2021-03-11  \n",
       "236     2023-01-24  "
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df[df.pipeline_tag == \"summarization\"].sort_values(by=\"downloads\", ascending=False).head(10)[[\"downloads\", \"id\", \"last_modified\"]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "387b4493-c283-4561-947c-c0de197f3e37",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
